/*
 * @Author: your name
 * @Date: 2021-08-16 19:30:10
 * @LastEditTime: 2021-08-26 18:54:44
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_study\src\router\index.js
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store';
import { Dialog } from "vant";

const Home = () =>
    import ('../views/home/Home.vue');
const Categroy = () =>
    import ('../views/categroy/Categroy.vue');
const Detail = () =>
    import ('../views/detail/Detail.vue');
const Profile = () =>
    import ('../views/profile/Profile.vue');
const ShopCart = () =>
    import ('../views/shopcart/ShopCart.vue');
const Register = () =>
    import ('../views/profile/Register.vue');
const Login = () =>
    import ('../views/profile/Login.vue');
const Address = () =>
    import ('../views/profile/Address.vue');
const AddressEdit = () =>
    import ('../views/profile/AddressEdit.vue');
const CreateOrder = () =>
    import ('../views/order/CreateOrder.vue');
const Order = () =>
    import ('../views/order/Order.vue');
const OrderDetail = () =>
    import ('../views/order/OrderDetail.vue');


const routes = [{
        path: '',
        name: 'DefaultHome',
        component: Home,
        meta: {
            title: '图书商城'
        }
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
            title: '图书商城-首页'
        }
    },
    {
        path: '/Categroy',
        name: 'Categroy',
        component: Categroy,
        meta: {
            title: '图书商城-商品分类'
        }
    },
    {
        path: '/detail',
        name: 'Detail',
        component: Detail,
        meta: {
            title: '图书商城-商品详情'
        }
    },
    {
        path: '/profile',
        name: 'Profile',
        component: Profile,
        meta: {
            title: '图书商城-个人中心',
            isAuthRequired: true
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        meta: {
            title: '图书商城-用户注册'
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '图书商城-用户登录'
        }
    },
    {
        path: '/shopcart',
        name: 'ShopCart',
        component: ShopCart,
        meta: {
            title: '图书商城-用户注册',
            isAuthRequired: true
        }
    },
    {
        path: '/address',
        name: 'Address',
        component: Address,
        meta: {
            title: '图书商城-地址管理',
            isAuthRequired: true
        }
    },
    {
        path: '/addressedit',
        name: 'AddressEdit',
        component: AddressEdit,
        meta: {
            title: '图书商城-地址编辑',
            isAuthRequired: true
        }
    },
    {
        path: '/createOrder',
        name: 'CreateOrder',
        component: CreateOrder,
        meta: {
            title: '图书商城-创建订单',
            isAuthRequired: true
        }
    },
    {
        path: '/order',
        name: 'Order',
        component: Order,
        meta: {
            title: '图书商城-订单支付',
            isAuthRequired: true
        }
    },
    {
        path: '/orderDetail',
        name: 'OrderDetail',
        component: OrderDetail,
        meta: {
            title: '图书商城-订单详情',
            isAuthRequired: true
        }
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    //登录时判断
    if (to.meta.isAuthRequired && store.state.user.isLogin === false) {
        Dialog.alert({
            message: '您还没有登录，请先登录'
        }).then(() => {
            // on close
        });
        next('/login')
    } else {
        next()
    }

    // next();
    document.title = to.meta.title
})

export default router